package cn.jinjimi.app.main.uicontroller;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceSelector;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.entity.AppConfig.AppAlert;
import cn.jinjimi.app.managers.ConfigManager;
import cn.jinjimi.app.main.dialog.AlertDialog;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

public class AlertViewController {

    private static final int AUTO_SHOW = 0;
    private static final int CLICK_SHOW = 1;

    private Activity mContext;

    //Views:
    private FrescoImageView mEntryView;
    private AlertDialog mDialog;

    //Data:
    private AppAlert mAlertData;
    private boolean hasShown = false;
    private String mModule;

    public AlertViewController(Activity context) {
        mContext = context;
    }

    public void setEntryButtonView(FrescoImageView v) {
        if (v != null) {
            mEntryView = v;
            mEntryView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideEntry();
                    showAlert(CLICK_SHOW);
                }
            });
        }
    }

    public void setAlertData(String module, AppAlert alert) {
        if (alert != null && module != null) {
            mModule = module;
            mAlertData = alert;
            alert();
        }
    }

    public void alert() {
        if (mDialog == null) {
            mDialog = getAlertDialog();
        }

        if (ConfigManager.getInstance().shouldDisplay(mAlertData)) {
            showAlert(AUTO_SHOW);
        } else {
            showEntry();
        }
    }

    @EventTrace({
            EventID.APP_SUPER_GUACARDSHOW,
            EventID.APP_SUPER_GUACARDCLICK
    })
    private void showAlert(@EventTraceSelector int auto) {
        if (mAlertData != null && mAlertData.content != null
                && !TextUtils.isEmpty(mAlertData.content.linkUrl)) {

            if (auto == 0) {
                ConfigManager.getInstance().markAlertHasShown(mAlertData);
            }
            mDialog.show(mAlertData.content.linkUrl);
        }
    }

    private AlertDialog getAlertDialog() {
        AlertDialog dialog = new AlertDialog(mContext);
        dialog.setOwnerActivity(mContext);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                ConfigManager.getInstance().fetchConfig(mModule);
                showEntry();
            }
        });
        return dialog;
    }

    private void showEntry() {
        if (!hasShown) {
            if (mAlertData != null && mAlertData.content != null
                    && !TextUtils.isEmpty(mAlertData.content.imageUrl)) {
                mEntryView.loadView(mAlertData.content.imageUrl);

                mEntryView.setVisibility(View.VISIBLE);

                ObjectAnimator an = ObjectAnimator.ofFloat(mEntryView, "translationX", mEntryView.getMeasuredWidth(), 0);
                an.setDuration(300);
                an.start();
                hasShown = true;
            }
        }

    }

    public void hideEntry() {
        if (hasShown) {
            ObjectAnimator an = ObjectAnimator.ofFloat(mEntryView, "translationX", 0, mEntryView.getMeasuredWidth());
            an.setDuration(300);
            an.start();
            hasShown = false;
        }
    }

    public void updateAlertInfo() {
        if (mEntryView != null
                && mEntryView.isShown()
                && mModule != null) {

            mAlertData = ConfigManager.getInstance().getAppAlert(mModule);

            //update image
            if (mAlertData != null
                    && mAlertData.content != null
                    && mAlertData.content.imageUrl != null) {
                mEntryView.loadView(mAlertData.content.imageUrl);
            }
        }
    }


}